package com.wisewin.circle.api.controller;

import com.wisewin.circle.api.controller.base.BaseController;
import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.OrgUserDO;
import com.wisewin.circle.service.OrgUserService;
import com.wisewin.circle.util.MD5Util;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/orgUser")
public class OrgUserController extends BaseController {
    final static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private OrgUserService orgUserService;

    // 登录
    @PostMapping("/login")
    public ResultDTO login (@RequestBody OrgUserDO orgUserDO){
        if(StringUtils.isEmpty(orgUserDO.getLoginName()) || StringUtils.isEmpty(orgUserDO.getPasscode())){
            return ResultDTOBuilder.failure("1", "参数有误", null) ;
        }
        // 查询用户
        OrgUserDO orgUser = orgUserService.queryByLoginName(orgUserDO.getLoginName(), MD5Util.digest(orgUserDO.getPasscode()));
        if(orgUser == null){
            return ResultDTOBuilder.failure("1", "用户名或密码有误", null) ;
        }
        String sessionId = super.setOrgLoginUser(orgUser) ;
        return ResultDTOBuilder.success(sessionId);
    }

    // 查询机构用户信息
    @PostMapping("/queryOrgUserInfo")
    public ResultDTO queryOrgUserInfo (HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        OrgUserDO orgUserDO = super.getOrgLoginUser(token);
        if (orgUserDO == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        orgUserDO = orgUserService.queryOrgUserById(orgUserDO.getId());
        return ResultDTOBuilder.success(orgUserDO);
    }

    // 查询首页统计数据
    @PostMapping("/queryHomeData")
    public ResultDTO queryHomeData (HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        OrgUserDO orgUserDO = super.getOrgLoginUser(token);
        if (orgUserDO == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return orgUserService.queryHomeData();
    }

    // 查询用户列表
    @PostMapping("/queryOrgUserList")
    public ResultDTO queryOrgUserList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        OrgUserDO orgUserDO = super.getOrgLoginUser(token);
        if (orgUserDO == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return orgUserService.queryOrgUserList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }
}
